const App = getApp();
import { url } from '../../../../modelApi/url_config.js'
Page({
    data: {
        statusBarHeight: "",
        statusBarPadding: "",
        upgradeType: null, // 选择的类型,
        phoneNumber: "", //手机号
        out_trade_no: '',
        grade: 0, //级别
    },
    onLoad: function (options) {
        wx.hideShareMenu();
        let upgradeType = '',
            phoneNumber= '',
            shareUserId = '';
        if (options.scene) {
            const scene = decodeURIComponent(options.scene)
            upgradeType = scene.split('&')[0]
            phoneNumber = scene.split('&')[1]
            shareUserId = scene.split('&')[2]
            wx.setStorageSync('shareUserId',shareUserId)
        } else {
            upgradeType = options.upgrade_type
            phoneNumber = options.phoneNumber
            shareUserId = options.shareUserId
            shareUserId && wx.setStorageSync('shareUserId',shareUserId)
        }
        if (!wx.getStorageSync("tel")) {
            this.goRegister(UrlData);
            return
        }
        this.setData({
            statusBarPadding: App.globalData.navTop,
            statusBarHeight: App.globalData.navHeight,
            upgradeType: upgradeType,
            phoneNumber: phoneNumber || '',
            grade: wx.getStorageSync('grade') || 0,
        });
    },
    /* 跳转登陆 */
    goRegister(UrlData) {
        wx.setStorageSync("backPage", UrlData)
         wx.navigateTo({
             url: "/pages/register/index"
         });
    },
    /* 立即购买 */
    goUpgradePay(event) {
        const that = this;
        if (+that.data.upgradeType === 4 && ~~that.data.grade >= 3) {
            return;
        }that
        if (+that.data.upgradeType === 5 && ~~that.data.grade >= 4) {
            return;
        }
        if (+that.data.upgradeType === 6 && ~~that.data.grade >= 5) {
            return;
        }
        let promoterId = wx.getStorageSync('shareUserId') ? wx.getStorageSync('shareUserId') : '';
        const param = {
            gradeId: event.currentTarget.dataset.type,
            promoterId: promoterId
        };
        App.ajax(url.upgradeUrl, param, 'post').then(res => {
            that.setData({
                out_trade_no: res.data.out_trade_no
            })
            wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                success: function (res1) {
                    that.getorderNo(that.data.out_trade_no);
                },
                fail: function (res1) {
                    wx.showToast({
                        title: '取消支付',
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
        })
    },
    // 获取轮循订单号
    getorderNo(data) {
        const param = { orderNo: data }
        App.ajax(url.getByNo, param, 'get').then(res => {
            const data = res.data;
            if (+res.code === 0 && data) {
                if (+data.payStatus === 1) {
                    wx.reLaunch({
                        url: `/pages/orderDetail/index?orderId=${data.orderId}`,
                    })
                    return;
                }
                this.getorderNo(this.data.out_trade_no)
            }
        })
    },
    // onShareAppMessage: function (res) {
    //     const url_data = "pages/upgradeList/sonpages/detail/index?upgrade_type=" + this.data.upgradeType + "&shareUserId=" + wx.getStorageSync('userId');
    //     console.log(url_data)
    //     //通过右上角菜单触发
    //     console.log(url_data)
    //     return {
    //         path: url_data,
    //     };
    // },
})